package com.lzhl.buildjar.ui;

import com.lzhl.buildjar.App;
import com.lzhl.buildjar.ui.support.Cmd;
import com.lzhl.buildjar.ui.support.CmdListener;
import com.lzhl.buildjar.ui.support.Setting;
import com.lzhl.buildjar.util.ImageUtils;
import com.lzhl.buildjar.util.UIUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2014/7/11
 */
public class ToolBarUI extends JToolBar implements CmdListener {
    private boolean showAdv;

    public ToolBarUI() {
        jbinit();
        init();
    }

    private void jbinit() {
        this.setRollover(true);
        showAdv = Setting.isShowAdv();

        buildTool(Cmd.CATEGORY_ADD);
        buildTool(Cmd.SCHEME_ADD);
        buildTool(Cmd.SCHEME_SAVE);
        buildTool(Cmd.SCHEME_REFRESH);
        buildTool(Cmd.SCHEME_DEL);
        buildTool(Cmd.SHOW_ADV);
        buildTool(Cmd.SETTING);
        buildTool(Cmd.TOOLS);

        buildSeparator();
        buildTool(Cmd.LINE_UP);
        buildTool(Cmd.LINE_DOWN);
        buildTool(Cmd.LINE_TOP);
        buildTool(Cmd.LINE_BOTTOM);
        buildTool(Cmd.LINE_ADD);
        buildTool(Cmd.LINE_DEL);
        buildTool(Cmd.LINE_CLEAR);

        buildSeparator();
        buildTool(Cmd.ADD_INNER);
        buildTool(Cmd.DEL_INNER);
        buildTool(Cmd.CLEAR_REPEAT);
        buildTool(Cmd.CHECK_SCHEME);
        buildTool(Cmd.BUILD_JAR);

        buildSeparator();
        buildTool(Cmd.HELP);
        buildTool(Cmd.ABOUT);
        buildTool(Cmd.LOGOUT);

        buildHide(Cmd.CATEGORY_FOCUS);
        buildHide(Cmd.SCHEME_FOCUS);
    }

    private void init() {
        App.registCmdListener(this);
    }

    class ToolAction extends AbstractAction {
        Cmd cmd;

        public ToolAction(Cmd cmd) {
            this.cmd = cmd;
        }

        public void actionPerformed(ActionEvent e) {
            Object data = null;
            if(Cmd.SHOW_ADV.equals(cmd)) {
                showAdv = !showAdv;
                data = showAdv;

                UIUtils.invoke(new Runnable() {
                    public void run() {
                        Setting.setShowAdv(showAdv);
                    }
                });
            }
            App.fireCmd(cmd, data);
        }
    };

    private JButton buildTool(Cmd cmd) {
        JButton btn;
        ImageIcon icon = ImageUtils.getImageIcon(cmd.getIcon());
        if(icon != null) {
            btn = new JButton(icon);
        }
        else {
            btn = new JButton(cmd.getName());
        }

        btn.setToolTipText(getTooltip(cmd));
        btn.setRequestFocusEnabled(false);
        btn.setFocusable(false);

        ToolAction action = new ToolAction(cmd);
        btn.addActionListener(action);
        if(cmd.getShortcutkey() != null && cmd.getShortcutkey().length() > 0) {
            KeyStroke keyStroke = KeyStroke.getKeyStroke(cmd.getShortcutkey());
            getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                    keyStroke, cmd.getCode() + "_shortcutkey");
            getActionMap().put(cmd.getCode() + "_shortcutkey", action);
        }

        this.add(btn);
        return btn;
    }

    private void buildHide(Cmd cmd) {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(cmd.getShortcutkey());
        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                keyStroke, cmd.getCode() + "_shortcutkey");
        getActionMap().put(cmd.getCode() + "_shortcutkey", new ToolAction(cmd));
    }

    private void buildSeparator() {
        this.addSeparator();
    }

    private String getTooltip(Cmd cmd) {
        if(cmd.getShortcutkey().trim().length() == 0) {
            return cmd.getName();
        }

        String[] strs = cmd.getShortcutkey().trim().split(" ");
        StringBuffer tooltip = new StringBuffer(cmd.getName());
        tooltip.append("(");
        for(String str : strs) {
            tooltip.append(str.substring(0, 1).toUpperCase() + str.substring(1) + "+");
        }
        tooltip.deleteCharAt(tooltip.length()-1);
        tooltip.append(")");

        return tooltip.toString();
    }

    @Override
    public Object handleCmd(Cmd cmd, Object data) {
        if(Cmd.SETTING.equals(cmd)) {
            new SettingUI((Frame)getTopLevelAncestor());
        }
        else if(Cmd.HELP.equals(cmd)) {
            new HelpUI((Frame)getTopLevelAncestor());
        }
        else if(Cmd.ABOUT.equals(cmd)) {
            new AboutUI((Frame)getTopLevelAncestor());
        }

        return null;
    }
}
